package com.example.poemtest.index.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.poemtest.R;

import java.util.List;


public class SearchAdapter extends BaseAdapter {
    private Context mContext;
    private int layoutResId;
    private List<String> list;


    public SearchAdapter(Context mContext, int layoutResId, List<String> list) {
        this.mContext = mContext;
        this.layoutResId = layoutResId;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (list != null) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId, null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.tvMaybeKnownName = convertView.findViewById(R.id.tv_maybe_known_name);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        }else {
            //重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvMaybeKnownName.setText(list.get(position));
        //给控件对象绑定要显示的数据
        Log.e("搜索框内容", list.get(position));
        Log.e("list", list.toString());
        return convertView;
    }

    private static class ViewHolder{
        //每一个item中的控件
        TextView tvMaybeKnownName;
    }
}

